const App = {
    mixins: [Query],//配置文件，路径/utils/mixins.js
    data() {
        return {
            getYMDHMSDate,
            photo,
            page: {
                activeName: 'first',
                value1: '',
                PageNumber: 1, //页码
                RowAmount: 10,
                Condition: window.btoa(unescape(encodeURIComponent("%%"))),
                ExtensionCondition:  window.btoa(unescape(encodeURIComponent("AND personal_information.RR_Village_Number IN (" +  getCookie('village')  + ")\r\n")))
            },
            str1: '',
            str: '',
            form: {},
            form1: {},
            fileList: []
        }
    },
    mounted() {

    },
    methods: {
        Treedata() {
            const _this = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/clientOperation/getDistrictTree`,
                type: "GET",
                data: {
                    DistrictCode: '232723'

                },
                success: (data) => {
                    this.treeData = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        handleNodeClick(data, node) {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + data.ProvinceNumber + "%,%" + this.page.value1 + "%")))
            this.postgetList(this.page);
        },
        check() {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%")))
            this.postgetList(this.page);
        },
        handlePoints(row) {
            window.location.href = "./points.html?action=Add&nodeKey=" + row.ID_Card_Number;
        },
        handleAddPoints(row) {
            this.form1 = {
                Type: '0',
                Points: 1,
                Children: {
                    personal_points_change_log_attachment: []
                }
            }
            this.form = row;
            this.showDialog = true;
        },
        handleSubPoints(row) {
            this.form1 = {
                Type: '1',
                Points: 1,
                Children: {
                    personal_points_change_log_attachment: []
                }

            }
            this.form = row;
            this.showDialog = true;
        },
        async validateAllForms() {
            //获取当前页面上所有的Elform
            let forms = [];
            for (let key in this.$refs) {
                const ref = this.$refs[key];
                if (ref.$options && ref.$options.name == 'ElForm') {
                    forms.push(ref);
                }
            }
            //判断每个Elform的校验是否通过
            let isfail = false;
            for (const form of forms) {
                try {
                    await form.validate();
                } catch (error) {
                    isfail = true;
                }
            }
            if (isfail) {
                return false;
            }
            return true;
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: 'personal_points_account',
                        Condition: `ID_Card_Number='${this.form.ID_Card_Number}' AND Points_Type='${this.form1.Points_Type}'`,
                    },
                    success: (data) => {
                        let lJSONObject_DataSet = {};
                        if (data.length == 1) {
                            if (this.form1.Type == '1') {//扣减
                                if (this.form1.Points <= data[0].Remain_Points) {
                                    data[0].Remain_Points = accSub(data[0].Remain_Points, this.form1.Points);
                                    data[0].Sales_Points = accAdd(this.form1.Points, data[0].Sales_Points);
                                    this.form1.Points = "-" + this.form1.Points;
                                    lJSONObject_DataSet = {
                                        Modify: {
                                            personal_points_account: data
                                        },
                                        Add: {
                                            personal_points_change_log: [{
                                                ID_Card_Number: this.form.ID_Card_Number,
                                                Points_Title:'管理员扣减' +(this.form1.Points_Type == '0'?'村民积分':'党员积分')+ '积分',
                                                Points_Type:this.form1.Points_Type,
                                                Change_Points: this.form1.Points,
                                                Remain_Points: data[0].Remain_Points,
                                                Notes:this.form1.Notes,
                                                Children:this.form1.Children
                                            }]
                                        }
                                    }
                                } else {
                                    this.$message({
                                        message: '积分不足，无法扣减',
                                        type: 'warning'
                                    });
                                    this.loading = false;
                                    return;
                                }
                            } else if (this.form1.Type == '0') {//新增
                                data[0].Remain_Points = accAdd(this.form1.Points, data[0].Remain_Points);
                                data[0].Total_Points = accAdd(this.form1.Points, data[0].Total_Points);

                                lJSONObject_DataSet = {
                                    Modify: {
                                        personal_points_account: data
                                    },
                                    Add: {
                                        personal_points_change_log: [{
                                            ID_Card_Number: this.form.ID_Card_Number,
                                            Points_Title:'管理员增加' +(this.form1.Points_Type == '0'?'村民积分':'党员积分')+ '积分',
                                            Points_Type:this.form1.Points_Type,
                                            Change_Points: this.form1.Points,
                                            Remain_Points: data[0].Remain_Points,
                                            Notes:this.form1.Notes,
                                            Children:this.form1.Children
                                        }]
                                    }
                                }
                            }
                        } else {
                            if (this.form1.Type == '1') {//扣减
                                this.$message({
                                    message: '积分不足，无法扣减',
                                    type: 'warning'
                                });
                                this.loading = false;
                                return;
                            } else {
                                this.form1.Points_Title = '管理员增加' +(this.form1.Points_Type == '0'?'村民积分':'党员积分')+ '积分'
                                lJSONObject_DataSet = {
                                    Add: {
                                        personal_points_change_log: [{
                                            ID_Card_Number: this.form.ID_Card_Number,
                                            Points_Title:'管理员增加' +(this.form1.Points_Type == '0'?'村民积分':'党员积分')+ '积分',
                                            Points_Type:this.form1.Points_Type,
                                            Change_Points: this.form1.Points,
                                            Remain_Points: this.form1.Points,
                                            Notes:this.form1.Notes,
                                            Children:this.form1.Children
                                        }],
                                        personal_points_account: [{
                                            ID_Card_Number: this.form.ID_Card_Number,
                                            Points_Type: this.form1.Points_Type,
                                            Total_Points: this.form1.Points,
                                            Remain_Points: this.form1.Points,
                                        }]
                                    }
                                }
                            }
                        }
                        let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                        lJSONObject_DataSet['Add']['base_operation_log'] = [{
                            Operation_Type: '0',
                            Operation_Path: this.relativePath,
                            Operation_Main,
                            Operation_Interface: "generalSaveByRequestBody",
                            Operation_IP: getIpAddress()
                        }]
                        submitSave(lJSONObject_DataSet, (res) => {
                            if (res.Status == 'success') {
                                this.postgetList(this.page);
                                this.form1 = {};
                                this.form = {};
                                this.showDialog = false;
                                this.loading = false;
                            }
                        })
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            }
        },
        success(row, res) {
            this.form1.Children.personal_points_change_log_attachment.push({
                uid: row.uid,
                Attachment_URL: res.fileName,
                Attachment_Name: row.name,
                Temp_Path: res.tempPath,
            })
        },
        remove(row) {
            if (!Object.keys(row).some(item => item.includes('_Original'))) {
                this.form1.Children.personal_points_change_log_attachment = this.form1.Children.personal_points_change_log_attachment.filter(i => i.uid != row.uid);
            } else {
                let obj = this.form1.Children.personal_points_change_log_attachment.filter(i => i.Attachment_SerialNumber == row.Attachment_SerialNumber)[0];
                obj.Row_Status = '1'
            }
        },
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
